package com.hydra.cron.server.cron.listener;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * hdyraCron Created by ZhengGong on 15/12/13.
 * PackageName com.hydra.cron.server.cron.listener
 * Description
 */
public abstract class AbstractChildNodeListener implements PathChildrenCacheListener {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractChildNodeListener.class);

    public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
        switch (pathChildrenCacheEvent.getType()) {
            case CHILD_ADDED:
                LOG.info("CHILD_ADDED: " + pathChildrenCacheEvent.getData().getPath());
                add(curatorFramework, pathChildrenCacheEvent);
                break;

            case CHILD_REMOVED:
                LOG.info("CHILD_REMOVED: " + pathChildrenCacheEvent.getData().getPath());
                removed(curatorFramework, pathChildrenCacheEvent);

                break;

            case CHILD_UPDATED:
                LOG.info("CHILD_UPDATED: " + pathChildrenCacheEvent.getData().getPath());
                update(curatorFramework, pathChildrenCacheEvent);

                break;

            case CONNECTION_LOST:
                LOG.info("CHILD_CONNECTION_LOST: " + pathChildrenCacheEvent.getData().getPath());
//                add(curatorFramework, pathChildrenCacheEvent);

                break;

            case CONNECTION_RECONNECTED:
                LOG.info("CHILD_CONNECTION_LOST: " + pathChildrenCacheEvent.getData().getPath());
//                add(curatorFramework, pathChildrenCacheEvent);

                break;

            case CONNECTION_SUSPENDED:
                LOG.info("CHILD_CONNECTION_LOST: " + pathChildrenCacheEvent.getData().getPath());
//                add(curatorFramework, pathChildrenCacheEvent);

                break;

            case INITIALIZED:
                LOG.info("CHILD_CONNECTION_LOST: " + pathChildrenCacheEvent.getData().getPath());
//                add(curatorFramework, pathChildrenCacheEvent);

                break;

            default:
                break;
        }
    }

    abstract public void add(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception;

    abstract public void removed(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception;

    abstract public void update(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception;
}
